
var express = require('express');
var ks3 = require('ks3');
var STS = ks3.STS
var app = express();

const { config: conf } = require('./const')

app.get('/', function (request, response) {


  var client = new STS({
    ak: conf.AccessKeyId,
    sk: conf.AccessKeySecret,
  });

  client.assumeRole({
    roleKrn: conf.RoleKrn,
    roleSessionName: 'test'
  }).then(result => {
    console.log(result)
    const res = JSON.parse(result)
    const credentials = res.AssumeRoleResult.Credentials
    response.set('Access-Control-Allow-Origin', '*');
    response.set('Access-Control-Allow-METHOD', 'GET');
    response.json({
      AccessKeyId: credentials.AccessKeyId,
      AccessKeySecret: credentials.SecretAccessKey,
      SecurityToken: credentials.SecurityToken,
      Expiration: credentials.Expiration
    });
  }).catch(err => {
    console.log(err);
    response.status(400).json(err.message);
  })
});

app.listen(8000, function () {
  console.log('----- ks3 sts server started. http://localhost:8000/ -----');
});